<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_user_login.php';
?>
<!DOCTYPE html>
<html>
    <head id="Head1">
        <title>
            Conferences (News Category) - Tesdo Admin
        </title>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        ?>
        <script type="text/javascript">
            function call() {
                document.getElementById('hiddenMsg').value = editor.getData();
            }
        </script>
    </head>
    <body>
        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'ckeditor.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_data_input.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_conferences.php';

        //Script Process Page
        //Get objects
        $dao_conferences = new dao_conferences();
        $lib_pager = new lib_pager();
        $lib_upload = new lib_upload();
        $lib_data_input = new lib_data_input();

        //-------------Form_Load-------------
        //CLEAR & CANCEL
        if (!empty($_POST['btnClear']) || !empty($_POST['btnCancel'])) {
            ?>
            <script type="text/javascript">
                window.location = <?php echo LINK_ROOT . DIR_ADMIN . 'conference.php'; ?>;
            </script>
            <?php
        } else {

            //POST process
            if ($_SERVER['REQUEST_METHOD'] == "POST") {
                $action_type = $_POST['a_type'];
                $conference_id = empty($_POST['a_conference_id']) ? 0 : $_POST['a_conference_id'];

                if ($action_type == 'delete' && $conference_id != 0) {
                    if ($dao_conferences->delete($conference_id) == 1) {
                        $success = 'Deleted conference with id = ' . $conference_id . ' successfully!';
                    } else {
                        $error = 'Delete conference with id = ' . $conference_id . ' failed!';
                    }
                } else if ($action_type == 'edit' && $conference_id != 0 && empty($_POST['btnSave'])) {
                    //Get Edit data when click on edit icon (not work with btnSave click)
                    $edit_item = $dao_conferences->get_by_id($conference_id);
//                var_dump($edit_item);
                } else if (empty($_POST['a_name'])
                        || empty($_POST['a_detail']) 
                        && !empty($_POST['btnSave'])) {
                    $error = "All the required field must not be empty!";
                } else {
                    //Get post array
                    $post_array = array(
                        'conference_name' => $_POST['a_name'],
                        'detail' => $_POST['a_detail'],
                    );
                    
                    $post_array['file_upload'] = "";

                    //--------------
                    if ($action_type == 'insert' || $action_type == 'edit') {
                        if (!empty($post_array['conference_name'])
                                && !empty($post_array['detail'])) {
//                            var_dump($post_array);
//                            echo $conference_id;
                            if ($dao_conferences->add($conference_id, 
                                    $post_array['conference_name'], 
                                    $post_array['detail'],
                                    $post_array['file_upload'])) {
                                if ($action_type == 'insert') {
                                    $success = 'Inserted new conference';
                                } else {
                                    $success = 'Updated information for conference with id = ' . $conference_id;
                                }
                            } else {
                                $error = 'Failed to progess the request!';
                            }
                        } else {
                            $error = 'Failed, the require field must be filled!';
                        }
                    }
                }
            }
        }
        //Get list and divide page
        if (!empty($_GET['p'])) {
            $current_page = $_GET['p'];
        } else {
            $current_page = 1;
        }
        $list_total = $dao_conferences->get_all();
        $page_size = 10;
        $total_page = $lib_pager->get_total_page($list_total, $page_size);
        $list_current = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);
        ?>

        <div>
            <div class="content-box-outer">
                <div class="content-box-right">
                </div>
                <div class="content-box-left">
                    <h1>Conferences (News Category)</h1>
                    <div class="MessageArea">
                        <?php if (!empty($error)) { ?>
                            <span id="cphAdmin_lblMsg" style="color: red;">
                                <?php echo $error; ?>
                            </span>
                        <?php } ?>
                        <?php if (!empty($success)) { ?>
                            <span id="cphAdmin_lblMsg" style="color: green;">
                                <?php echo $success; ?>
                            </span>
                        <?php } ?>
                    </div>
                    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" id="form_conference" onsubmit="call()" method="POST" enctype="multipart/form-data">
                        <div>
                            <input name="a_conference_id" value="<?php
                            if (empty($edit_item)) {
                                echo '0';
                            } else {
                                echo $edit_item['conference_id'];
                            }
                            ?>" type="hidden">
                            
                            <input name="a_type" value="<?php
                            if (empty($edit_item)) {
                                echo 'insert';
                            } else {
                                echo 'edit';
                            }
                            ?>" type="hidden">

                            <div class="TitleArea"><b>Conference Name:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input name="a_name" value="<?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['conference_name'];
                                }
                                ?>" type="text" id="a_name" style="width:400px;">
                            </div>
                            
                            <div class="TitleArea"><b>Detail:<span style="color: red;">*</span></b></div>
                            <div class="textBoxArea">
                                <input type="hidden" name="hiddenMsg" value="">
                                <textarea class="ckeditor" id="a_detail" name="a_detail">
                                <?php
                                if (empty($edit_item)) {
                                    echo '';
                                } else {
                                    echo $edit_item['detail'];
                                }
                                ?>
                                </textarea>
                            </div>
                            
                            <div class="TitleArea"><b>.</b></div>
                            <div class="textBoxArea">
                                <input type="submit" name="btnSave" value="Save" class="buttonDesign">
                                <input type="submit" name="btnClear" value="Clear" class="buttonDesign">
                                <input type="submit" name="btnCancel" value="Cancel" class="buttonDesign">
                            </div>
                        </div>

                        <br clear="all"><br>
                        <h1>Manage Conferences</h1>
                        <div>
                            <div>
                                <table cellspacing="0" cellpadding="4" rules="cols" id="cphAdmin_gvJournal" style="color:Black;background-color:White;border-color:#DEDFDE;border-width:1px;border-style:None;width:988px;border-collapse:collapse;">
                                    <tbody><tr style="color:White;background-color:#6B696B;font-weight:bold;">
                                            <th scope="col">ID</th>
                                            <th scope="col">Conference Name</th>
                                            <th scope="col">Detail</th>
                                            <th scope="col">Actions</th>
                                        </tr>
                                        <?php
                                        $c = 0;
                                        foreach ($list_current as $i => $item) {
                                            if ($c % 2 == 0) {
                                                ?>
                                                <tr style="background-color:#F7F7DE;"> 
                                                <?php } else { ?>
                                                <tr style="background-color:#FFF;"> 
                                                    <?php
                                                }
                                                ?>
                                                <td style="width:50%;">
                                                    <?php echo $item['conference_id']; ?>
                                                </td>
                                                <td style="width:50%;">
                                                    <?php echo $item['conference_name']; ?>
                                                </td>
                                                <td style="width:50%;">
                                                    <?php echo $item['detail']; ?>
                                                </td>
                                                <td align="center">
                                                    <input type="image" 
                                                           name="btnEdit"
                                                           title="Edit" 
                                                           onclick="set_value('a_type', 'edit');
                                                                       set_value('a_conference_id', <?php echo $item['conference_id']; ?>);
                                                                       submit_form('form_conference');" 
                                                           class="input-edit-image" 
                                                           src="<?php echo LINK_ROOT . DIR_ADMIN_LAYOUT_IMAGE . 'edit.png'; ?>" >
                                                    <input type="image" 
                                                           name="btnDelete" 
                                                           title="Delete" 
                                                           onclick="if (my_confirm()) {
                                                                           set_value('a_type', 'delete');
                                                                           set_value('a_conference_id', <?php echo $item['conference_id']; ?>);
                                                                           submit_form('form_conference');
                                                                       } else {
                                                                           return false;
                                                                       }"
                                                           class="input-delete-image" 
                                                           src="<?php echo LINK_ROOT . DIR_ADMIN_LAYOUT_IMAGE . 'delete.png'; ?>">
                                                </td>
                                            </tr>
                                            <?php
                                            $c++;
                                        }
                                        ?>
                                        <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                            <td colspan="5">
                                                <table>
                                                    <tbody>
                                                        <tr>
                                                            <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                                                <td>
                                                                    <span>
                                                                        <a href="conferences.php?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                    </span>
                                                                </td>
                                                            <?php } ?>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        </div>

        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
        ?>
    </body>
</html>
